<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Db;
class Usergroup extends Base
{
    public function index()
    {

        $data=$this->request->param();
        if(isset($data['ajax'])&&$data['ajax']==1){
            $map='';
            //$map=$this->search($data);
            $page=isset($data['page'])?$data['page']:1;
            $page=intval($page);
            $limit=isset($data['rows'])?$data['rows']:10;
            $limit=intval($limit);
            $start=$limit*($page-1);
          
            if($map==''){
                $list =Db::name('admin_group')->order('sort desc,id desc')->limit($start,$limit)->select();
                $count=Db::name('admin_group')->order('sort desc,id desc')->count();
            }else{
                $list =Db::name('admin_group')->order('sort desc,id desc')->limit($start,$limit)->select();
                $count=Db::name('admin_group')->order('sort desc,id desc')->count();
            }
            foreach ($list as $key => &$val) {
                $val['addtime']=date('Y-m-d',$val['addtime']);  
                $val['adminnum']=Db::name('admin')->where('group_id',$val['id'])->count();
                $val['status']=1;  
            }
            //$count =count($list);      
            return $this->json_data($count,$list); 

        }
        return $this->fetch();
    }
    //编辑栏目1-1
    public function add($id)
    {
        if(empty($id)){
            $info=[];           
        }else{
            $info=Db::name('admin_group')->where('id',$id)->find();
        }
        $this->assign('info',$info);     
        return $this->fetch('add');
    }
     //编辑栏目1-2
    public function save()
    {
         $status=0;
         $msg="操作失败";
         $data = $this->request->param();
        
         if(empty($data['id'])){
             $update=[
                 'name' => $data['name'],
                 'sort' => $data['sort'],
                 'addtime'=>time(),
                 'updatetime'=>time(),
              ];
              //执行操作
             $res=Db::name('admin_group')->insert($update);
             if ($res) {
                    adminlog('添加管理员组','添加');
                    $status=1;
                    $msg="更新成功";
              }
         }else{
             $id=$data['id'];
            
             $update=[
                 'name' => $data['name'],
                 'sort' => $data['sort'],
                 'updatetime'=>time(),
              ];
             //执行更新操作
             $res=Db::name('admin_group')->where('id', $id)->update($update);      
             if ($res) {
                    adminlog('修改管理员组','修改');
                    $status=1;
                    $msg="更新成功";
              }
         }
         return ['status'=>$status, 'msg'=> $msg];
    }


    //删除栏目1-1
    public function del($id){       
        $status=0;
        $msg="操作失败";
        if(empty($id)){
            $info=[];           
        }else{
            // $del=Db::name('admin_group')->where('id',$id)->delete();
            // if($del){
            //     $status=1;
            //     $msg="删除成功";
            // }

            $info=Db::name('admin_group')->where('id',$id)->find();
            $flag=Db::name('admin')->where('group_id',$info['id'])->count();
            if($flag>0){
                $msg="操作失败,请先删除组内人员";
            }else{
                $del=Db::name('admin_group')->where('id',$id)->delete();
                if($del){
                    adminlog('删除管理员组','删除');
                    $status=1;
                    $msg="删除成功";
                }
            }
        }      
        return ['status'=>$status, 'msg'=> $msg];
          
    }

     public function admin($id)
    {
        $this->assign('id',$id);
        $data=$this->request->param();
        if(isset($data['ajax'])&&$data['ajax']==1){
            $map='';
            //$map=$this->search($data);
            $page=isset($data['page'])?$data['page']:1;
            $page=intval($page);
            $limit=isset($data['rows'])?$data['rows']:10;
            $limit=intval($limit);
            $start=$limit*($page-1);
          
            if($map==''){
                $list =Db::name('admin')->where('group_id',$id)->order('sort desc,id desc')->limit($start,$limit)->select();
                $count=Db::name('admin')->where('group_id',$id)->order('sort desc,id desc')->count();
              
            }else{
                $list =Db::name('admin')->where('group_id',$id)->order('sort desc,id desc')->limit($start,$limit)->select();
                $count=Db::name('admin')->where('group_id',$id)->order('sort desc,id desc')->count();
            }
            foreach ($list as $key => &$val) {
                $val['addtime']=date('Y-m-d',$val['addtime']);  
                $val['company_name']=Db::name('company')->where('id',$val['company'])->value('name');  
                $val['group']=Db::name('admin_group')->where('id',$val['group_id'])->value('name');  
            }
            //$count =count($list);      
            return $this->json_data($count,$list); 

        }
        return $this->fetch('admin');
    }
}
